package com.five.dao.impl.backstage;

import com.five.dao.backstage.RoleDao1;
import com.five.entity.backstage.TRole;
import com.five.util.DBUtil;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @Author: 越川豪
 * @Date: 2020/11/25 15:27
 */
@Repository
public class RoleDao1Impl implements RoleDao1 {

    //分页查询
    @Override
    public List<Map> getAll( TRole tRole,Integer page, Integer limit) {
        String sql = "SELECT r.*, COUNT(s.roleid) a from t_role r left join t_staff s on r.id=s.roleid where 1=1 ";
        if(tRole.getId()!=null){
            sql+=" and r.id="+tRole.getId();
        }
        if(tRole.getName()!=null){
            sql+=" and r.name like '%"+tRole.getName()+"%'";
        }
        sql += "GROUP BY r.id limit ?,?";
        return DBUtil.queryMap(sql, (page - 1) * limit, limit);
    }

    //查询数据的条数
    @Override
    public int getAllCount(TRole tRole,Integer page, Integer limit) {
        String sql1=" SELECT r.*, COUNT(s.roleid) aaa from t_role r left join t_staff s on r.id=s.roleid where 1=1  ";
        if(tRole.getId()!=null){
            sql1+=" and r.id="+tRole.getId();
        }
        if(tRole.getName()!=null){
            sql1+=" and r.name like '%"+tRole.getName()+"%'";
        }
        sql1 += "GROUP BY r.id ";
        //group by r.id
        String sql="select count(*) a from ( "+sql1+" ) aa";
        List<Map> maps = DBUtil.queryMap(sql);
        Object a = maps.get(0).get("a");
        int i = Integer.parseInt(a + "");
        return i;
    }

    //删除
    @Override
    public int delRole(TRole tRole) {
        String sql = "delete from t_role where id=?";
        return DBUtil.manage(sql,tRole.getId());
    }

    //修改和添加
    @Override
    public int updOrAdd(TRole tRole) {
        if(null!=tRole.getId()){
            String sql="update t_role set name=? where id=?";
            return DBUtil.manage(sql,tRole.getName(),tRole.getId());
        }else {
            String sql="insert into t_role (name) values(?)";
            return DBUtil.manage(sql,tRole.getName());
        }
    }

    /* 分配权限 */
    @Override
    public int deleteRoleMenu(Integer rid) {
        String sql = "delete from t_role_menu where rid=?";
        return DBUtil.manage(sql,rid);
    }

    @Override
    public int addRoleMenu(String s,Integer rid) {
        String sql = "insert into t_role_menu(mid,rid) values(?,?)";
        return DBUtil.manage(sql,s,rid);
    }

    @Override
    public List<Map> getMenuByRid(Integer rid) {
        String sql = "select m.* from t_role_menu rm join t_menu m on m.mid=rm.mid where rm.rid=? and mpid!=0";
        return DBUtil.queryMap(sql,rid);
    }


    @Override
    public int bad(Integer id) {
        String sql="update t_role set status=1 where id=?";
        int i = DBUtil.manage(sql, id);
        return i;
    }

    @Override
    public int good(Integer id) {
        String sql="update t_role set status=0 where id=?";
        int i = DBUtil.manage(sql, id);
        return i;
    }

}
